<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/sys-admin/product/breed' }">宠物管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加宠物</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="宠物类别" prop="breedId">
            <el-radio v-model="radio" value="全部" label="1" @change="getRadioVal">猫</el-radio>
            <el-radio v-model="radio" value="部分" label="2" @change="getRadioVal">狗</el-radio>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/sys-admin/product/breed')">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {

      radio: '',

      ruleForm: {
        parentId:'',
        name: '',
        enable: 1,
        isParent: 0
      },
      rules: {

        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
getRadioVal(){
  console.log(this.radio);
},
    submitForm(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/breed/add-new';
          console.log('url = ' + url);
          this.ruleForm.parentId=JSON.stringify(this.radio*1);
          let formData = this.qs.stringify(this.ruleForm)
          console.log('formData = ' + formData);

          let localJwt = localStorage.getItem('localJwt');
          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt
                }
              })
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                message: '添加类别成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
    },

  }
}
</script>